package cn.edu.gdgm.system.service.impl;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.WxMaUserService;
import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
import cn.edu.gdgm.system.domain.WeChatProfile;
import cn.edu.gdgm.system.entity.WechatUserinfo;
import cn.edu.gdgm.system.exception.WeChatUserClientException;
import cn.edu.gdgm.system.mapper.WechatUserinfoMapper;
import cn.edu.gdgm.system.service.IUserService;
import cn.edu.gdgm.system.service.IWechatUserinfoService;
import cn.fightingguys.security.web.wechat.entity.WeChatMiniProgramUserDetails;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 药盒服务用户绑定微信小程序用户信息 服务实现类
 * </p>
 *
 * @author running_hyk
 */
@Service
public class WechatUserinfoServiceImpl extends ServiceImpl<WechatUserinfoMapper, WechatUserinfo> implements IWechatUserinfoService {

    private IUserService userService;

    private WxMaService wxMaService;

    @Override
    public void updateProfile(WeChatMiniProgramUserDetails userDetails, WeChatProfile weChatProfile) throws WeChatUserClientException {
        String openId = userDetails.getOpenId();

        /* 验证用户上传信息数据 */
        WxMaUserService wxMaUserService = wxMaService.getUserService();
        boolean verify = wxMaUserService.checkUserInfo(userDetails.getSessionKey(), weChatProfile.getRawData(), weChatProfile.getSignature());
        if (!verify) {
            throw new WeChatUserClientException("用户 UserProfile 无效");
        }
        WxMaUserInfo userInfo = wxMaUserService.getUserInfo(userDetails.getSessionKey(), weChatProfile.getEncryptedData(), weChatProfile.getIvStr());
        WechatUserinfo wechatUserinfo;


        /* 判断用户是否已经注册 */
        boolean userExist = userService.existUserByWeChatOpenId(openId);
        if (!userExist) {
            userService.createUserByWeChatOpenId(openId);
            wechatUserinfo = new WechatUserinfo();
        } else {
            wechatUserinfo = this.getBaseMapper().selectById(openId);
        }


        /* 将数据转换成数据库实体 */
        convertWechatUserinfo(userInfo, wechatUserinfo, userDetails.getSessionKey());
        wechatUserinfo.setUpdateTime(LocalDateTime.now());

        /* 存入数据库 */
        this.saveOrUpdate(wechatUserinfo);
    }

    @Autowired
    public WechatUserinfoServiceImpl setUserService(IUserService userService) {
        this.userService = userService;
        return this;
    }

    @Autowired
    public WechatUserinfoServiceImpl setWxMaService(WxMaService wxMaService) {
        this.wxMaService = wxMaService;
        return this;
    }

    private void convertWechatUserinfo(WxMaUserInfo source, WechatUserinfo target, String session) {
        target.setOpenId(source.getOpenId());
        target.setNickName(source.getNickName());
        target.setGender(source.getGender());
        target.setCity(source.getCity());
        target.setProvince(source.getProvince());
        target.setCountry(source.getCountry());
        target.setAvatarUrl(source.getAvatarUrl());
        target.setSession(session);
    }
}
